//
//  FoodGoodsTableViewCell.m
//  FaceCard
//
//  Created by 瞿伦平 on 2016/10/17.
//  Copyright © 2016年 瞿伦平. All rights reserved.
//

#import "FoodGoodsTableViewCell.h"

@interface FoodGoodsTableViewCell ()
@property(nonatomic,strong) UIView*        jianView;
@property(nonatomic,strong) UIView*        addView;
@end


@implementation FoodGoodsTableViewCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier isCartCell:(BOOL)isCartCell
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code.
        self.selectionStyle=UITableViewCellSelectionStyleNone;
        [self.contentView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
        self.backgroundColor = [UIColor colorWithRed:0.996 green:1.000 blue:1.000 alpha:1.000];
        
        float padding = 10;
        UIFont *font = [UIFont systemFontOfSize:15];
        UIView *superView = self.contentView;
        
        float btnWidth = 25;
        
        self.nameLable = [superView newUILableWithText:@"" textColor:[UIColor blackColor] font:[UIFont systemFontOfSize:14]];
        self.nameLable.numberOfLines = 0;
        
        self.priceLable = [superView newUILableWithText:@"" textColor:[UIColor redColor] font:font];
        
        UIButton *aView = [superView newUIButtonWithTarget:self mehotd:@selector(jianCountMethod:) imgNormal:IMG(@"矩形-21.png")];
        self.countLable = [superView newUILableWithText:@"" textColor:[UIColor blackColor] font:font textAlignment:QU_TextAlignmentCenter];
        UIButton *bView = [superView newUIButtonWithTarget:self mehotd:@selector(addCountMethod:) imgNormal:IMG(@"矩形-21-拷贝.png")];
        self.jianBtn = aView;
        self.addBtn = bView;
        
//        UIView *aView = ({
//            UIView *view = [superView newUIView];
//            UITapGestureRecognizer* singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTapJian:)];
//            singleRecognizer.numberOfTapsRequired = 1; // 单击
//            [view addGestureRecognizer:singleRecognizer];
//            
//            UILabel *lable = [view newUILableWithText:@"-" textColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:18] textAlignment:QU_TextAlignmentCenter];
//            lable.backgroundColor = [UIColor colorWithRed:0.518 green:0.753 blue:0.125 alpha:1.000];
//            lable.layer.cornerRadius = 3;
//            lable.layer.masksToBounds = YES;
//            [lable makeConstraints:^(MASConstraintMaker *make) {
//                make.centerY.centerX.equalTo(view);
//                make.width.height.equalTo(35);
//            }];
//            view;
//        });
//        UIView *bView = ({
//            UIView *view = [superView newUIView];
//            UITapGestureRecognizer* singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTapAdd:)];
//            singleRecognizer.numberOfTapsRequired = 1; // 单击
//            [view addGestureRecognizer:singleRecognizer];
//            
//            UILabel *lable = [view newUILableWithText:@"+" textColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:18] textAlignment:QU_TextAlignmentCenter];
//            lable.backgroundColor = [UIColor colorWithRed:0.518 green:0.753 blue:0.125 alpha:1.000];
//            lable.layer.cornerRadius = 3;
//            lable.layer.masksToBounds = YES;
//            [lable makeConstraints:^(MASConstraintMaker *make) {
//                make.centerY.centerX.equalTo(view);
//                make.width.height.equalTo(30);
//            }];
//            view;
//        });
        self.jianView = aView;
        self.addView = bView;
        
        if (isCartCell) {
            
            [self.nameLable makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(superView.left).offset(padding);
                make.top.bottom.equalTo(superView);
            }];
            [self.priceLable makeConstraints:^(MASConstraintMaker *make) {
                make.left.greaterThanOrEqualTo(_nameLable.right).offset(padding/2);
                make.width.equalTo(70);
                make.top.bottom.equalTo(superView);
            }];
            
            [bView makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(superView);
                make.right.equalTo(superView.right).offset(-padding/2);
                make.width.equalTo(btnWidth);
            }];
            [aView makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.centerY.equalTo(bView);
                make.right.equalTo(_countLable.left);
                make.left.greaterThanOrEqualTo(self.priceLable.right).offset(padding/2);
            }];
            [self.countLable makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(btnWidth);
                make.right.equalTo(bView.left);
                make.top.bottom.equalTo(aView);
            }];
            
        } else {
            self.thumbImgView = [superView newUIImageViewWithImg:IMG(@"DefaultImg.png")];
            self.saleNumLable = [superView newUILableWithText:@"月销量34" textColor:[UIColor grayColor] font:[UIFont systemFontOfSize:11]];
            
            [self.thumbImgView makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(superView.left).offset(padding);
                make.top.equalTo(superView.top).offset(padding);
                make.bottom.equalTo(superView.bottom).offset(-padding);
                //make.width.equalTo(_thumbImgView.mas_height);
                make.width.equalTo(_thumbImgView.mas_height).multipliedBy(FoodGoodsImageWidthWithHeight);
            }];
            [self.nameLable makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_thumbImgView.right).offset(padding/2);
                make.right.equalTo(superView.right).offset(-padding);
                //make.top.equalTo(_thumbImgView.mas_top);
                make.top.equalTo(superView.top).offset(padding/2);
                
                
                //make.bottom.equalTo(_thumbImgView.mas_centerY);
            }];
            //
            //self.saleNumLable.backgroundColor = [UIColor orangeColor];
            self.saleNumLable.numberOfLines = 0;
            [self.saleNumLable makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_nameLable.left);
                make.top.equalTo(_nameLable.bottom);
                make.right.equalTo(_nameLable.right);
                make.height.equalTo(26);
            }];
            //
            [self.priceLable makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_nameLable.left);
                make.top.equalTo(_saleNumLable.bottom);
                //make.bottom.equalTo(_thumbImgView.mas_bottom);
                make.bottom.equalTo(superView.bottom).offset(-padding/2);
                //make.height.equalTo(_nameLable.height).multipliedBy(0.6);
            }];
            
            [bView makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(self.priceLable);
                make.right.equalTo(superView.right).offset(-padding/2);
                make.width.equalTo(btnWidth);
                make.height.equalTo(20);
            }];
            [aView makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.centerY.equalTo(bView);
                make.right.equalTo(_countLable.left);
                make.left.greaterThanOrEqualTo(self.priceLable.right).offset(padding/2);
            }];
            [self.countLable makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(btnWidth);
                make.right.equalTo(bView.left);
                make.top.bottom.equalTo(aView);
            }];
        }
        

        
        self.count = 0;
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

-(void)loadUIWithItem:(FoodGoods *)item
{
    self.nameLable.text = item.name.length>0 ? item.name : @"暂无";
    self.saleNumLable.text = item.introduction.length>0 ? item.introduction : @"暂无";
    //NSLog(@"introduction:%@",item.introduction);
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],item.img];
    [self.thumbImgView setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:IMG(@"timg.png")];
    self.priceLable.text = [NSString strRMBWithMoney:item.price];
}


-(void)setCount:(int)count
{
    _count = count;
    
    [self loadUIWithCount];
}


-(void)loadUIWithCount
{
    if (_count > 0) {
        self.countLable.hidden = NO;
        self.jianView.hidden = NO;
    } else {
        self.countLable.hidden = YES;
        self.jianView.hidden = YES;
    }
    self.countLable.text = [NSString stringWithFormat:@"%i", _count];
}

-(void)SingleTapJian:(UITapGestureRecognizer*)recognizer
{
    [self jianCountMethod:nil];
}

-(void)SingleTapAdd:(UITapGestureRecognizer*)recognizer
{
    [self addCountMethod:nil];
}

-(void)jianCountMethod:(UIButton *)sender
{
    if (_count > 0) {
        self.count --;
        
        if (self.jianCallBack)
            self.jianCallBack(_count, self);
    }
}


-(void)addCountMethod:(UIButton *)sender
{
    self.count ++;
    
    if (self.addCallBack)
        self.addCallBack(_count, self);
}


@end


